﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.ServiceModel;
using System.ServiceModel.Description;
using System.Net;
using System.Web;
using System.IO;
using System.Threading;
using System.Collections;

namespace RunGate
{
    [ServiceContract]
    public interface TCPConnection
    {
        [OperationContract]
        bool RecieveMessage(string messageid, string message);
    }

    public class TCPConnectionService : TCPConnection
    {

        public bool Connect(string serverip, int gateport, int serverport)
        {
            try
            {
                ServiceHost host = null;
                string urlService = "";

                IPHostEntry ips = Dns.GetHostEntry(Dns.GetHostName());

                IPAddress _ipAddress = ips.AddressList[0];
                urlService = ("net.tcp://" + serverip + ":" + serverport.ToString());

                host = new ServiceHost(typeof(TCPConnectionService));

                NetTcpBinding tcpBinding = new NetTcpBinding();
                tcpBinding.TransactionFlow = false;
                tcpBinding.Security.Transport.ProtectionLevel = System.Net.Security.ProtectionLevel.EncryptAndSign;
                tcpBinding.Security.Transport.ClientCredentialType = TcpClientCredentialType.Windows;
                tcpBinding.Security.Mode = SecurityMode.None;

                host.AddServiceEndpoint(typeof(TCPConnection), tcpBinding, urlService);

                host.Open();

                return true;
            }
            catch (Exception)
            {
                return false;
            }
        }


        public bool RecieveMessage(string messageid, string message)
        {
            return true;
        }

    }
}
